import {addStudentAPI, getAllStudentAPI, modStudentAPI} from "@/network/student/student";
import {ElMessage} from "element-plus";
import {defineStore} from "pinia";
import {ref} from "vue";

export const useStudentStore = defineStore('Student', ()=>{
  const studentList = ref([]);
  const totalCount = ref(0)
  const totalPages = ref(1)
  const getStudentLoading = ref(false)
  const studentDrawerLoading = ref(false)

  //这里不要使用箭头函数，否则为Student_list赋值时，会提示this指向型错误，this会指向undefined
  function getStudentList(data) {
    getStudentLoading.value = true
    if(!data){
      data={
        skip: 1,
        limit: 10,
      }
    }
    getAllStudentAPI(data).then(res=>{
      getStudentLoading.value = false
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0 && res.data.data){
        totalCount.value = res.data.total_count
        totalPages.value = res.data.total_pages
        studentList.value = res.data.data
      }
    }).catch(error=>{
      ElMessage.error(error.message)
    })
  }

  function addStudent(data){
    studentDrawerLoading.value = true
    addStudentAPI(data).then(res=>{
      studentDrawerLoading.value = false
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0){
        this.toggleDrawer()
        this.getStudentList()
        return ElMessage.success("添加成功")
      }
    }).catch(error=>{
      return ElMessage.error(error.message)
    })
  }

  function modStudent(data){
    studentDrawerLoading.value = true
    modStudentAPI(data).then(res=>{
      studentDrawerLoading.value = false
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0){
        this.toggleDrawer()
        this.getStudentList()
        return ElMessage.success("修改成功")
      }
    }).catch(error=>{
      return ElMessage.error(error.message)
    })
  }

  const drawer = ref(false);
  const studentInfo = ref({
    pay_amount: 0,
    loan_amount: 0
  })
  function toggleDrawer (data) {
    if(data && data !== {}){
      this.studentInfo = {...data}
      if (this.studentInfo.address) {
        this.studentInfo.address = JSON.parse(this.studentInfo.address)
      }
    }
    this.drawer = !this.drawer
  }

  return {
    studentList,
    totalCount,
    totalPages,
    getStudentList,
    getStudentLoading,
    addStudent,
    modStudent,
    studentDrawerLoading,
    drawer,
    studentInfo,
    toggleDrawer
  }
})